/**
 * create by ck on 2016/7/13.
 */
$(document).ready(function () {
    var list = $('#List');
    var form = $("#XH_Form_Main");
    var type = xhTool.getQuery('type');
    $('.XH-DataGrid-Toolbar').parseToolbar();
    $('.XH-Form-Main').parseForm($('#bt_Form_Add, #bt_Form_Edit'));
    list.initDataGrid(baseUrl + 'Buy/getExcellentStudent').setSelectThenEnable(['bt_List_Del']);
    /** 列表 **/
    $('#bt_List_Add').click(function(){
        $.overtFun.clickAddList(list);
    });
    $('#bt_List_Del').click(function () {
        var ids = list.getDataGridSelectedFields('iFAQId');
        $.overtFun.clickDelList(ids);
    });
    if($('#bt_Form_Edit').length > 0){
        DataGrid.onDblClickRow = function(rowIndex, rowData){
            $.post(baseUrl + 'Other/getModelInfoById', {id: rowData.id, target:   'Excellent_Student'}, function(data){
                $.overtFun.clickAddList();
                $('#bt_Form_Add, #bt_Form_Reset').hide();
                $('#bt_Form_Edit').show();
                form.fill(data).form.find('[must=true]').attr('disabled', true).change();
                // $("#XH_Form_Main").xhForm('fillFormData', data).find('[must=true]').attr('disabled', true).change();
            }, 'json');
            return false;
        };
    }
    /** 表单 **/
    $('#bt_Form_Add, #bt_Form_Edit').click(function(){
        $.overtFun.clickSubmitForm();
    });
    $('#bt_Form_Back').click(function(){
        $.overtFun.clickBackForm();
    });
    $('#bt_Form_Reset').click(function(){
        $.overtFun.clickResetForm();
    });
    $('#bt_FileUpload_Back').click(function(){
        $('#div_file').hide();
        $('.XH-List').parent().fadeIn();
    });

    //选中封面自动上传
    var coverUrlDom = $('#avatar');
    rebindUploadCover();
    function rebindUploadCover()
    {
        var coverImgDom = $('#cover_img');
        coverImgDom.on('change', function(){
            coverUrlDom.val('').attr('placeholder', '上传中，请稍后...');
            $.ajaxFileUpload({
                url: baseUrl + 'Read/uploadArticleImage',
                secureuri:false,
                fileElementId: coverImgDom[0].id,                               //file标签的id  
                dataType: 'json',                                               //返回数据的类型  
                success: function (data, status) {
                    if(data.error) {
                        window.top.swal({
                            title: data.error,
                            type: 'error'
                        });
                        return false;
                    }
                    coverUrlDom.val(data.url);
                    rebindUploadCover();
                },
                error: function (data, status, e) {
                    window.top.swal({
                        title: e,
                        type: 'error'
                    });
                    rebindUploadCover();
                }
            });
        });
    }


});


